package com.gary.security.entity;

import java.io.Serializable;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

import com.gary.core.orm.utils.BaseEntity;

@Entity
@Table(name = "ROLE")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Role extends BaseEntity implements Serializable {

    static final long serialVersionUID = 0L;

    private String name;

    private String descn;
    
    private Set<User> users = new HashSet<User>(0);

    private Set<Resc> rescs = new HashSet<Resc>(0);

    private Set<Menu> menus = new HashSet<Menu>(0);

    public Role() {
    }

    @GenericGenerator(name = "generator", strategy = "increment")
    @Id
    @GeneratedValue(generator = "generator")
    @Column(name = "ID", unique = true, nullable = false)
    public String getId() {
        return id;
    }

    @Column(name = "NAME", length = 50, unique = true, nullable = false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "DESCN", length = 200)
    public String getDescn() {
        return descn;
    }

    public void setDescn(String descn) {
        this.descn = descn;
    }

    @ManyToMany(cascade =  {
        CascadeType.PERSIST, CascadeType.MERGE}
    , fetch = FetchType.LAZY)
    @JoinTable(name = "USER_ROLE", joinColumns =  {
        @JoinColumn(name = "ROLE_ID")
    }
    , inverseJoinColumns =  {
        @JoinColumn(name = "USER_ID")
    }
    )
    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    @ManyToMany(cascade =  {
        CascadeType.PERSIST, CascadeType.MERGE}
    , fetch = FetchType.LAZY)
    @JoinTable(name = "RESC_ROLE", joinColumns =  {
        @JoinColumn(name = "ROLE_ID")
    }
    , inverseJoinColumns =  {
        @JoinColumn(name = "RESC_ID")
    }
    )
    public Set<Resc> getRescs() {
        return rescs;
    }

    public void setRescs(Set<Resc> rescs) {
        this.rescs = rescs;
    }

    @ManyToMany(cascade =  {
        CascadeType.PERSIST, CascadeType.MERGE}
    , fetch = FetchType.LAZY)
    @JoinTable(name = "MENU_ROLE", joinColumns =  {
        @JoinColumn(name = "ROLE_ID")
    }
    , inverseJoinColumns =  {
        @JoinColumn(name = "MENU_ID")
    }
    )
    @OrderBy("theSort")
    public Set<Menu> getMenus() {
        return menus;
    }

    public void setMenus(Set<Menu> menus) {
        this.menus = menus;
    }

    /**
     * 判断相同.
     *
     * @param o Object
     * @return boolean
     */
    public boolean equals(Object o) {
        if (!(o instanceof Role)) {
            return false;
        } else {
            Role role = (Role) o;

            return role.id.equals(this.id);
        }
    }

    public int hashCode() {
        if (this.id == null) {
            return super.hashCode();
        } else {
            return this.id.hashCode();
        }
    }
}
